package com.codemes.happylist.modules.json;

import de.felixroske.jfxsupport.FXMLController;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
import javafx.scene.web.WebView;
import javafx.util.Duration;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.net.URL;
import java.util.ResourceBundle;
/**
 * @author nscoffee
 */
@FXMLController
@Slf4j
public class JsonFormatController implements Initializable {
    public VBox hboxContainer;
    public WebView mediaPlayWebView;

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {

        Button button = new Button();
        button.setText("play");
        button.setPrefWidth(100);
        button.getStyleClass().add("btn-info");
        button.setOnAction(event -> {
            String localFilePath = "https://staticedu-wps.cache.iciba.com/audio/128b797c65d27081004aa529bc21360e.mp3";
            String localFilepath = "/tmp/happy_list/iciba/fab3faa8d303f0eba394ee81451520e5.mp3";
            File mp3File = new File(localFilepath);
            log.info("点击播放按钮，播放英文音频 > {}",localFilePath);
            //"http://download.oracle.com/otndocs/products/javafx/oow2010-2.flv"
            String htmlPage = "/htmlPages/mediaPlayer.html";
            log.info("page->{}",JsonFormatController.class.getResource(htmlPage).toString());
            mediaPlayWebView.getEngine().load(JsonFormatController.class.getResource(htmlPage).toString());
            mediaPlayWebView.getEngine().getLoadWorker().stateProperty().addListener(new ChangeListener<Worker.State>() {
                @Override
                public void changed(ObservableValue<? extends Worker.State> observableValue, Worker.State state, Worker.State t1) {
                    log.info("observableValue={} state->{} t1={}",observableValue,state,t1);
                    log.info("message-{}",mediaPlayWebView.getEngine().getLoadWorker().getMessage());
                    log.info("e->{}",mediaPlayWebView.getEngine().getLoadWorker().getException());
                }
            });
        });
        hboxContainer.getChildren().add(button);
    }
}
